package queue;

public class LinkedQueue {

    private DoubleNode m_pHeadNode;
    private DoubleNode m_pTailNode;

    public void enqueue(Object objData) {
        DoubleNode pNewNode = new DoubleNode();
        pNewNode.setData(objData);

        if (this.m_pTailNode == null) {
            this.m_pHeadNode = pNewNode;
            this.m_pTailNode = pNewNode;

        } else {
            this.m_pTailNode.setNextNode(pNewNode);
            this.m_pTailNode = pNewNode;
        }
    }

    public Object dequeue() {
        if (this.m_pHeadNode == null) {
            return null;
        }

        Object objResult = this.m_pHeadNode.getData();
        this.m_pHeadNode = this.m_pHeadNode.getNextNode();
        if (this.m_pHeadNode == null) {
            this.m_pTailNode = null;
        }

        return objResult;
    }

    public void printAll() {
        if (this.m_pHeadNode == null) {
            System.out.println("empty");
        } else {
            StringBuilder sb = new StringBuilder();
            DoubleNode pDoubleNode = this.m_pHeadNode;
            while (pDoubleNode != null) {
                sb.append(pDoubleNode.getData() + ",");
                pDoubleNode =pDoubleNode.getNextNode();
            }
            System.out.println(sb.toString());
        }
    }

}
